import {
    offLogin,
    getUserInfo
} from '../api/user';

const state = {
    userMsg: JSON.parse(localStorage.getItem('myToken')) || {},
    userInfo: {},
};
const actions = {
    //退出登录
    async offLogin({
        state,
        commit
    }, data) {
        // let res = await offLogin(data);
        // if (res.data.code === 0) {
        localStorage.removeItem('myToken');
        state.userMsg = {}; //一定记得清空vuex,否则不能及时驱动页面刷新
        // }
    },
    //获取用户信息
    async UserInfo({
        commit
    }, id) {
        let res = await getUserInfo(id);
        if (res.status === 200) {
            commit('USERINFO', res.data); //res.data||
        }
    },
};
const mutations = {
    //登录
    setUser(state, data) {
        state.userMsg = data;
        localStorage.setItem('myToken', JSON.stringify(data));
    },
    //获取用户信息
    USERINFO(state, data) {
        state.userInfo = data;
    }
};
const getters = {};
export default {
    state,
    actions,
    mutations
}